package king.dominic.jlibrary.adapter;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import king.dominic.jlibrary.activity.OriginActivity;
import king.dominic.jlibrary.fragment.OriginFragment;
import king.dominic.jlibrary.util.FragmentFactory;

/**
 * Created by king on 2017/4/1.
 * OriginFragmentPagerAdapter
 *
 * @see king.dominic.jlibrary.util.FragmentFactory
 */

public class OriginFragmentPagerAdapter extends FragmentPagerAdapter {
    /**
     * @hide
     */
    private final OriginFragment[] fragments;
    private final FragmentFactory.FragmentParent parent;
    private final Class<? extends OriginFragment>[] fragmentClasses;

    /**
     * 强烈不建议使用
     *
     * @param fm        FragmentManager
     * @param fragments fs
     */
    @Deprecated
    public OriginFragmentPagerAdapter(FragmentManager fm, OriginFragment... fragments) {
        super(fm);
        this.parent = null;
        this.fragments = fragments;
        fragmentClasses = null;
    }

    /**
     * @param parent    当前所在的Activity
     * @param fragments fragment页面
     * @see king.dominic.jlibrary.util.FragmentFactory
     */
    public OriginFragmentPagerAdapter(FragmentActivity parent, OriginFragment... fragments) {
        super(parent.getSupportFragmentManager());
        this.parent = parent instanceof FragmentFactory.FragmentParent? (FragmentFactory.FragmentParent) parent :null;
        this.fragments = fragments;
        fragmentClasses = null;
    }

    /**
     * @param parent    当前所在的Fragment
     * @param fragments fragment页面
     * @see king.dominic.jlibrary.util.FragmentFactory
     */
    public OriginFragmentPagerAdapter(Fragment parent, OriginFragment... fragments) {
        super(parent.getChildFragmentManager());
        this.parent = parent instanceof FragmentFactory.FragmentParent? (FragmentFactory.FragmentParent) parent :null;
        this.fragments = fragments;
        fragmentClasses = null;
    }

    /**
     * @param parent  当前所在的Activity
     * @param classes fragment的类
     * @see king.dominic.jlibrary.util.FragmentFactory
     */
    @SafeVarargs
    public OriginFragmentPagerAdapter(OriginActivity parent, Class<? extends OriginFragment>... classes) {
        super(parent.getSupportFragmentManager());
        this.parent = parent;
        fragments = new OriginFragment[classes.length];
        fragmentClasses = classes;
    }

    /**
     * @param parent  当前所在的Fragment
     * @param classes fragment的类
     * @see king.dominic.jlibrary.util.FragmentFactory
     */
    @SafeVarargs
    public OriginFragmentPagerAdapter(OriginFragment parent, Class<? extends OriginFragment>... classes) {
        super(parent.getChildFragmentManager());
        this.parent = parent;
        fragments = new OriginFragment[classes.length];
        fragmentClasses = classes;
    }

    @Override
    public Fragment getItem(int position) {
        return fragment(this, position);
    }

    @Override
    public int getCount() {
        return fragments.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        final OriginFragment.FragmentTitle ft = fragment(this, position).getFragmentTitle();
        if (ft == null) return null;
        if (ft.textId == 0 || parent == null || !(parent instanceof Activity || parent instanceof Fragment))
            return ft.text;
        if (parent instanceof Activity)
            return ((Activity) parent).getString(ft.textId);
        return ((Fragment) parent).getString(ft.textId);
    }

    private static OriginFragment fragment(OriginFragmentPagerAdapter adapter, int position) {
        final OriginFragment f = adapter.fragments[position];
        if (f == null && adapter.parent != null && adapter.fragmentClasses != null) {
            adapter.fragments[position] = FragmentFactory.obtain(adapter.parent, adapter.fragmentClasses[position]);
            return adapter.fragments[position];
        }
        return f;
    }

    public OriginFragment[] getFragments() {
        initFragments();
        return fragments;
    }

    private void initFragments() {
        for (int i = 0; i < fragments.length; i++) {
            fragment(this, i);
        }
    }
}
